package qeug.shsh.fi.flowPlugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.engine.extitf.IWorkflowPlugin;
import qeug.shsh.helper.FacadeHelper;
import qeug.shsh.webService.WebServiceUtil;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 借款单工作流调用G6接口
 */
public class DailyLoanflowPlugin implements IWorkflowPlugin {

    @Override
    public void notify(AgentExecution execution) {
        try {
            IWorkflowPlugin.super.notify(execution);
            String fid = execution.getBusinessKey();//单据id
            String entityNumber = execution.getEntityNumber();//单据标识
            DynamicObject bill = BusinessDataServiceHelper.loadSingle(fid, entityNumber, "");
            SortedMap<String, Object> map = new TreeMap<String, Object>();
            if (bill != null) {
                map.put("ERPID", fid);//唯一标识符
                map.put("ERPNO", bill.get("billno"));//唯一标识编号
                map.put("FMCLSNO", "EPMS004");//付款业务类型
                map.put("CURRENCYNO", bill.getDynamicObjectCollection("accountentry").
                        get(0).getDynamicObject("accountcurrency").get("number"));//币种
                map.put("PAYTYPE", bill.get("qeug_paytype"));//付款类别
                map.put("PAYCLTNAME", bill.getDynamicObject("costdept").get("name"));//付款单位名称
                map.put("CUSTNAME", bill.getDynamicObject("costcompany").get("name"));//付款所属部门名称
                map.put("RECACTNO", bill.getDynamicObjectCollection("accountentry").get(0).get("payeraccount"));//收款方账号
                map.put("RECNAME", bill.getDynamicObjectCollection("accountentry").get(0).get("payeraccountname"));//收款方户名
                map.put("RECBANKNO", bill.getDynamicObjectCollection("accountentry").
                        get(0).getDynamicObject("payerbank").get("number"));//收款方支行编码
                map.put("RECBANKNAME", bill.getDynamicObjectCollection("accountentry").get(0).
                        getDynamicObject("payerbank").get("name"));//收款方支行名称
                map.put("RELCORPNO", "EPMS004");//客商编码
                map.put("PAYACTDATE", bill.getDate("repaymentdate"));//期望付款日期
                map.put("AMOUNT", bill.getBigDecimal("approveamount "));//付款金额
                map.put("ISPERSON", 1);//对公对私标识
                map.put("EXPLAIN", bill.get("description"));//摘要（用途）
                map.put("KMNAME", bill.getDynamicObject("qeug_kmname").get("name"));//款项用途（名称）
//            map.put("ITEMTID","EPMS004");//资金计划行项目号
                map.put("PAYINNER", 0);//是否内转
                map.put("URGENTFLAG", 0);//加急标识
//            map.put("CONTRACTNO",0);//业务合同编号
//            map.put("MEMO",bill.getDynamicObjectCollection("").get(0).get(""));//备注
                map.put("PAYPERSON", bill.getDynamicObject("applier").get("name"));//付款经办人
                map.put("PAYDATE", bill.getDate("bizdate"));//制单日期
                map.put("CHANNEL", "CCS");//付款单发起渠道
                map.put("SETTLEMENTMODES", "0");//结算方式
                map.put("STCODE", bill.get("qeug_paytype"));//付款方式
                map.put("BUDGETFLAG", bill.get("qeug_budgetflag"));//预算类型
                map.put("FMCLS", 2);//业务类型
                map.put("ISWAIT", 0);//是否等通知
//            map.put("PROJECTNAME",0);//投资项目
                map.put("DIR", 0);//主/被动付款
                map.put("DOCTYPE", 2);//单据类型
                String toXml = FacadeHelper.mapToXml(map, "data", "item");
                String xml = FacadeHelper.perfectXml(toXml, bill);
                String json = WebServiceUtil.invokeG6Pay("http://172.30.18.188:7081/FDLKF_OUTER/webservice/TxServiceGateway", xml);
                System.out.println(json);
            }
        } catch (
                RemoteException | ServiceException | MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
}
